package mobi.hami.huapin.dao;

import mobi.hami.huapin.daobase.CrudMapper;
import mobi.hami.huapin.dto.UserOrderDTO;
import mobi.hami.huapin.model.UserOrder;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Repository;

import java.math.BigDecimal;
import java.util.List;


/**
 * Create by 小峰 on 2018/9/1.
 */
@Repository
public interface UserOrderMapper extends CrudMapper<UserOrder> {

    @Select("select * from user_order where huapin_id=#{id} and order_status!=1")
    List<UserOrderDTO> getListByHuapinId(Integer id);

    @Select("select * from user_order where user_id=#{id} and order_status!=1")
    List<UserOrderDTO> getListByUserId(Integer id);

    /**
     * 获取昨天16点到当前时间未付款的订单
     * @return
     */
    @Select("SELECT  * from user_order where order_status=1 and " +
            "create_time> date_format(date_add(now(), interval-1 day), '%Y-%m-%d 16:00:00')")
    List<UserOrderDTO> getYesterdayUnPayList();

    @Select("select * from user_order where id=#{id}")
    UserOrderDTO getOneById(Integer Id);

    @Update("update user_order  set order_status=#{orderStatus},update_time=now(),pay_money=#{money}" +
            " where order_code=#{orderCode};")
    int updateOrderPayStatusByOrderId(@Param("orderCode") String orderCode,@Param("money") BigDecimal money,@Param("orderStatus")Integer orderStatus);

    @Select("select * from user_order where order_code=#{orderCode};")
    UserOrder getOrderByCode( String orderCode);


    @Select("CALL InvokeCollage();")
    List<UserOrder> taskCallInvokeCollage();

    @Update("update user_order  set update_time=now(),order_status=#{orderStatus} where id=#{orderId}")
    int updateSendStatus(@Param("orderId")Integer orderId, @Param("orderStatus")Integer orderStatus);

    /**
     * 根据地址和用户Id　获取购买次数
     * @param userid
     * @param addressId
     * @return
     */
    @Select("select count(0) from user_order a\n" +
            "join hua_pin b on a.huapin_id=b.id\n" +
            "where order_status not in (1,3) and a.user_id=#{userId} and b.address_id=#{addressId} ")
    int findCountByuserIdAndAddressid(@Param("userId")Integer userid,@Param("addressId")Integer addressId);


    /**
     * 更新收货状态(当收货时间超过24小时后,默认更新为已接收)
     * @return
     */
    @Select("call UpdateSendStatus();")
    Void taskUpdateSendStatus();
}
